/*
 * Copyright 2007,2008 Anton Dignoes, Daniel Rampanelli, Martin Palma
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.webduff.store.compound;

import java.io.InputStream;
import java.util.Date;
import java.util.Map;

import com.googlecode.webduff.MimeTyper;
import com.googlecode.webduff.io.URI;
import com.googlecode.webduff.store.WebdavStore;


public class RootWebDuffStore implements WebdavStore {
	
	private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(RootWebDuffStore.class);
	
	private Map<String, WebdavStore> theDefinedStores;
	
	public RootWebDuffStore(Map<String, WebdavStore> definedStores) {
		theDefinedStores = definedStores;
	}

	public void begin() {
		
	}

	public void commit() {
		
	}

	public void createFolder(URI uri) {
		
	}

	public void createResource(URI uri) {
		
	}

	public String[] getChildrenNames(URI uri) {
		return theDefinedStores.keySet().toArray(new String[]{});
	}

	public Date getCreationDate(URI uri) {
		return new Date();
	}

	public Date getLastModified(URI uri) {
		return new Date();
	}

	public InputStream getResourceContent(URI uri) {
		return null;
	}

	public long getResourceLength(URI uri) {
		return 0;
	}

	public boolean isFolder(URI uri) {
		log.debug("Nested rootStore.isFolder(" + uri + ")");
		String firstToken = uri.getNthComponent(0);
		return (firstToken != null && theDefinedStores.containsKey(firstToken)) || (uri.size() == 0);
	}

	public boolean isResource(URI uri) {
		return false;
	}

	public boolean objectExists(URI uri) {
		log.debug("Nested rootStore.objectExists(" + uri + ")");
		return isFolder(uri);
	}

	public void removeObject(URI uri) {
	}

	public void rollback() {
	}

	public void setResourceContent(URI uri,
			InputStream content, String contentType,
			String characterEncoding) {
	}

	public boolean canCreateFolder(URI uri) {
		return false;
	}

	public boolean canCreateResource(URI uri) {
		return false;
	}

	public String getMimeType(URI uri) {
		String firstToken = uri.getNthComponent(0);
		if (firstToken != null && theDefinedStores.containsKey(firstToken)) {
			return MimeTyper.FOLDER_MIMETYPE;
		}
		return null;
	}

}